if (!exists("app_root")) {
  stop("Need to have app_root defined to source this template");
}

source(paste(app_root,"cgi-lib/R/ep.io.R",sep="/"))

library(Biobase)
library(genefilter)

ep.anova <- function ( dataset, factor, fdr ) {
  eset<-ep.readBinExprSet(dataset)
  Afilter <- Anova ( phenoData(eset)[[factor]], p=fdr )
  aff <- filterfun ( Afilter )
  
  try ( w <- genefilter ( exprs(eset), aff ) )

  if ( !exists("w") ) {
    return ( .Last.value );
  }
  
  return ( which(w==TRUE) );
}

library(limma)
ep.ebayes <- function ( dataset, factor, gene_number ) {
  eset <- ep.readBinExprSet(dataset)
  design <- model.matrix ( ~ -1 + eset[[factor]] )
  colnames(design) <- levels ( eset[[factor]] )

  fit <- lmFit ( eset, design )
  fit <- eBayes ( fit )

  # MK (30/08/06) changed to return the F-statistic ordering
  # instead of topTable's default (coef=1)
  return ( order ( fit$F.p.value )[1:gene_number] )
}
